package com.wholeintech.dao;

import java.util.ArrayList;
import java.util.List;

import com.wholeintech.entity.Navigation;
import com.wholeintech.utils.CommonUtil;
import com.wholeintech.entity.DeviceDb;
//import com.wit.smartutils.DatabaseUtils;
//import com.wit.smartutils.entity.BoxInfo;
//import com.wit.smartutils.entity.Navigation;
import android.content.Context;
import android.database.Cursor;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * 瀵艰埅鏁版嵁鎿嶄綔绫�
 *
 * @author Administrator
 */
public class NavigationDao {
    private final static String TAG = "NavigationDao";
    private Context mContext = null;

    public NavigationDao(Context context) {
        this.mContext = context;
    }

    /**
     * 鏂板
     *
     * @param navigation
     * @return
     */
    public boolean add(Navigation navigation) {
        boolean bRet = false;
        DbManager db = CommonUtil.MyDbUtils();
        try {
            db.save(navigation);
            bRet = true;
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return bRet;
    }

    /**
     * 鍒犻櫎淇℃伅
     *
     * @param intId
     */
    public void delete(int intId) {
        DbManager db = CommonUtil.MyDbUtils();

        try {
            db.deleteById(Navigation.class, intId);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 鍒犻櫎淇℃伅
     */
    public void deleteAll() {
        DbManager db = CommonUtil.MyDbUtils();

        try {
            db.delete(Navigation.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 鑾峰彇鎵�鏈夌洅瀛愪俊鎭�
     *
     * @return
     */
    public List<Navigation> getNavigationList() {
        DbManager db = CommonUtil.MyDbUtils();
        List<Navigation> list = null;

        try {
            list = db.findAll(Navigation.class);
            if (list != null) {
                list.size();
            }
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return list;
    }

    /**
     * 鑾峰彇鍏锋湁閿氱偣鐨勫鑸�
     *
     * @return
     */
    public List<Navigation> getAnchorList() {
        DbManager db = CommonUtil.MyDbUtils();
        List<Navigation> list = null;

        try {
//            list = db.findAll(Selector.from(Navigation.class).where("anchor","!=",""));
            list = db.selector(Navigation.class).where("anchor", "!=", "").findAll();
            if (list != null) {
                list.size();
            }
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return list;
    }

    /**
     * 浠庡鑸〃涓幏鍙栨墍鏈夌數鍣�
     *
     * @return
     */
    public List<DeviceDb> getAllDeviceList() {
        List<DeviceDb> deviceList = null;
        DeviceDb device = null;

        Cursor cursor = null;
        DbManager db = CommonUtil.MyDbUtils();
        List<Navigation> list = null;
        StringBuilder sb = new StringBuilder();
        sb.append("select a.[devId],a.[name],a.[type],b.[macAddr] from Navigation a left join DeviceDatabase b ");
        sb.append("on a.[devId]=b.devId where (a.anchor is null or a.anchor='')");

        try {
            int intDevIdIndex;
            int intNameIndex;
            int intTypeIndex;
            int intMacAddrIndex;

            cursor = db.execQuery(sb.toString());
            if (cursor != null) {
                deviceList = new ArrayList<DeviceDb>();
                while (cursor.moveToNext()) {
                    device = new DeviceDb();
                    intDevIdIndex = cursor.getColumnIndex("devId");
                    intNameIndex = cursor.getColumnIndex("name");
                    intTypeIndex = cursor.getColumnIndex("type");
                    intMacAddrIndex = cursor.getColumnIndex("macAddr");

                    device.setName(cursor.getString(intNameIndex));
                    device.setDevId(cursor.getString(intDevIdIndex));
                    device.setType(cursor.getInt(intTypeIndex));
                    device.setMacAddr(cursor.getString(intMacAddrIndex));
                    deviceList.add(device);
                }
            }

        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return deviceList;
    }

    /**
     * 鑾峰彇鎸囧畾瀵艰埅淇℃伅
     * @param strChar
     * @return
     */
	/*
    public Navigation getNavigationById(String strId){
        DbUtils db = CommonUtil.MyDbUtils(mContext);
        Navigation info = null;
        
        try 
        {
            info = db.findFirst(Selector.from(BoxInfo.class).where("id", "=", strId));            
        } catch (DbException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try{
                db.close();
                db = null;
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }   
        
        return info;
    }*/
}
